(function() {
	var ics = Iefgames_CS;
	if ($defined(ics.Message)) {
		return ;
	}
	
	ics.Message = function() {
		var imPanelsId = 'ICS_ImPanels';

		var HtmlConf = {
			'csWindows': 'cs_windows',
			
			'csWindow': 'cs_window',
			'csHistory': 'cs_history',
			'csInput': 'cs_input',
			'csSend': 'cs_send',
			'csClose': 'cs_close',
			
			'csIframe': 'cs_iframe',
			'csForm': 'cs_form',
			'csFrom': 'cs_msgFrom',
			'csTo': 'cs_msgTo',
			'csMsg': 'cs_msg'
		}
	
		var usUuid, csUuid;
		var port = 8080;
		var open = function() {
			if (!$(HtmlConf.csWindows)) {
				$(document.body).grab(new Element('div', {
					id: HtmlConf.csWindows
				}));
			}
			
			if (!$(HtmlConf.csWindow)) {
				$(HtmlConf.csWindows).innerHTML = getCsWindowStr();
			}
			
			$(HtmlConf.csSend).addEvent('click', send);
			$(HtmlConf.csClose).addEvent('click', close);
			$(HtmlConf.csInput).addEvent('click', function(event) {
				//TODO: 未测试
				if (event.key == 13) {
					event.stop();
					event.stopPropagation();
					
					send(event.target);
				}
			});
			
			ics._('start');
		}
		
		var close = function(ev) {
			//TODO: 未完全测试
			var usUuid = ev.target.id.replace(HtmlConf.csClose + '_', '');
			$(HtmlConf.csWindow).distory();
			
		}
		
		var send = function(ev) {
			if (!$(HtmlConf.csForm)) {
				ready();
			}			
			
			//消息为空
			if (!$(HtmlConf.csInput).value.trim()) {
				return;
			}
			
			$(HtmlConf.csForm).setProperties({
				method: 'post',
				action: ics.Core.baseUri + (port == 80 ? '' : ':' + port) + '/msg'
			});
			
			$(HtmlConf.csTo).value = Cookie.read(ics.Cookie.csUuidCookieName);
			$(HtmlConf.csFrom).value = Cookie.read(ics.Cookie.uuidCookieName);
			$(HtmlConf.csMsg).value = ics._('base64Encode', $(HtmlConf.csInput).value.trim());
			
			$(HtmlConf.csForm).submit();
			
			//清空输入框
			$(HtmlConf.csInput).value = '';

			$(HtmlConf.csInput).focus();
		}
		
		var State = {
			'100': '',
			'200': '',
			'300': '',
			'400': '',
			'500': '',
			'600': '',
			'601': '对方下线',
			'602': '您已离线'
		}
		
		var callback = function(code) {
			if (!State[code]) {
				return;
			}
			
			var historyEl = $(HtmlConf.csHistory);
			historyEl.innerHTML += State[code];
			historyEl.scrollTop += historyEl.scrollHeight;
		}
	
		var getCsWindowStr = function() {
			var str = '';
			str += '<div id="' + HtmlConf.csWindow + '" class="chat">\n';
			str += '\t<div id="' + HtmlConf.csHistory  + '" class="chat_content">\n';
			str += '\t\t<ul>\n';
			str += '\t\t</ul>\n';
			str += '\t</div>\n';
			str += '\t<div class="chat_q">\n';
			str += '\t\t<div class="chat_input">\n';
			str += '\t\t\t<textarea id="' + HtmlConf.csInput + '" rows="2">你好啊</textarea>\n';
			str += '\t\t</div>\n';
			str += '\t\t<div class="chat_button"><button id="' + HtmlConf.csSend + '" title="发送消息">发送</button>\n';
			str += '\t\t<br /><button id="' + HtmlConf.csClose + '" title="关闭当前窗口">关闭</button>\n';
			str += '\t\t</div>\n';
			str += '\t</div>\n';
			str += '</div>\n';
			return str;
		}
		
		//创建iframe及form供提交消息之用
		var ready = function() {
			$(document.body).grab(new Element('div').set('html',
				'<iframe id="' + HtmlConf.csIframe + '" name="' + HtmlConf.csIframe + '" src="about:blank">'));
			
			var str = 
			'<form id="' + HtmlConf.csForm + '" target="' + HtmlConf.csIframe + '">'
				+ '<input name="to" id="' + HtmlConf.csTo + '" type="text" />'
				+ '<input name="from" id="' + HtmlConf.csFrom + '" type="text" />'
				+ '<input name="msg" id="' + HtmlConf.csMsg + '" type="text" />'
			+ '</form>';
			$(document.body).grab(new Element('div').set('html', str));
		}
		
		
		
		var json2Html = function(json) {
			var message = '';
			var usUuid = Cookie.read(ics.Cookie.uuidCookieName);
			if (usUuid == json.fromUuid) {
				message = '<li class="t_color12"><strong>我</strong>:' + ics._('base64Decode', json.message) + '</li>';
			} else if (usUuid == json.toUuid) {
				message = '<li class="t_color12"><strong>客服</strong>:' + ics._('base64Decode', json.message) + '</li>';
			}
			
			return message;	
		}
		
		var show = function(msgs) {
			var str = '';
			
			try {
				if (msgs.length != 0) {
					for (var i=0; i<msgs.length; ++i) {
						show(msgs[i]);
					}
				}
			} finally {	}
			
			
			if ($type(msgs) == 'array') {
			} else if ($type(msgs) == 'object') {
				str += json2Html(msgs);
			}
			
			
			var historyEl = $(HtmlConf.csHistory);
			historyEl.innerHTML += str;
			historyEl.scrollTop += historyEl.scrollHeight;
		}
		
		ics._$('msg', show);
		
		ics._$('open', open);
		
		ics._$('callback', callback);
		
		return {
			'show': show
		}
	}()
	
})()